﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DLL;

namespace BLL
{
    public static class examDesign_BLL
    {
        //创建数据连接
        private static dbExamDataContext db = new dbExamDataContext();
        /// <summary>
        /// 获取所有已有试卷数据源
        /// </summary>
        public static object GetSource()
        {
            return from p in db.ExamPaper orderby p.KM.kmNum,p.examVer select new { p.KM.kmName, p.examNum, p.examName, p.totalScore, p.eligibleScore, p.examVer, p.examType,p.timeOver, bFB = (p.bFB == true ? "是" : "否") };
        }

        /// <summary>
        /// 获取所有已有试卷数据源
        /// </summary>
        public static object GetSource(string examNum)
        {
            return from p in db.ExamPaper where p.examNum==decimal.Parse(examNum) orderby p.KM.kmNum, p.examVer select new {p.kmNum, p.KM.kmName, p.examNum, p.examName, p.totalScore, p.eligibleScore, p.examVer, p.examType, p.timeOver, bFB = (p.bFB == true ? "是" : "否") };
        }

        /// <summary>
        /// 获取所有已发布的试卷
        /// </summary>
        public static object GetFaBuExamPaper()
        {
            return from p in db.ExamPaper orderby p.KM.kmNum, p.examVer where p.bFB==true select new { p.KM.kmName, p.examNum, p.examName, p.totalScore, p.eligibleScore,p.timeOver, p.examVer, p.examType, bFB = (p.bFB == true ? "是" : "否") };
        }

        /// <summary>
        /// 获取前N条数据
        /// </summary>
        /// <param name="n">要获取的条数</param>
        public static object GetTopNPaper(int n)
        {
            object obj = new object();
            obj = (from p in db.ExamPaper orderby p.KM.kmNum ascending, p.examVer descending where p.bFB == true select new { p.KM.kmName, p.examNum, p.examName, p.totalScore, p.eligibleScore, p.timeOver, p.examVer, p.examType, bFB = (p.bFB == true ? "是" : "否"),img="~/Images/new.gif" }).Take(n);
            return obj;
        }

        /// <summary>
        /// 添加新的试卷
        /// </summary>
        /// <param name="newPaper">新的试卷数据对象</param>
        public static bool Add(ExamPaper newPaper)
        {
            //添加成功后的标签
            bool bl = false;
            try
            {
                if (db.ExamPaper.Single(p => p.examName == newPaper.examName).examNum > -1)
                    return false;
            }
            catch { }
            db.ExamPaper.InsertOnSubmit(newPaper);
            db.SubmitChanges();
            bl = true;
            return bl;
        }

        /// <summary>
        /// 更新试卷数据项
        /// </summary>
        /// <param name="paperNum">需要更新的试卷数据项标识</param>
        /// <param name="newPaper">新的试卷数据项</param>
        public static bool Update(string paperNum, ExamPaper newPaper)
        {
            //更新成功后的标签
            bool bl = false;
            try
            {
                //找到需要更新的数据项
                ExamPaper paper = db.ExamPaper.Single(p => p.examNum == decimal.Parse(paperNum));
                //更新相应数据
                paper.examType = newPaper.examType;
                paper.eligibleScore = newPaper.eligibleScore;
                paper.examName = newPaper.examName;
                paper.examVer = newPaper.examVer;
                paper.bFB = newPaper.bFB;
                paper.kmNum = newPaper.kmNum;
                paper.totalScore = newPaper.totalScore;
                paper.timeOver = newPaper.timeOver;
                //提交更新
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }

        /// <summary>
        /// 删除试卷数据项
        /// </summary>
        /// <param name="paperNum">要删除的试卷数据的标识</param>
        public static bool Delete(string paperNum)
        {
            //更新成功后的标签
            bool bl = false;
            try
            {
                db = new dbExamDataContext();
                //找到要删除的数据
                ExamPaper paper = db.ExamPaper.Single(p => p.examNum == decimal.Parse(paperNum));
                //准备数据
                db.ExamPaper.DeleteOnSubmit(paper);
                //提交更新
                db.SubmitChanges();                
                bl = true;
            }
            catch { }

            return bl;
        }

        /// <summary>
        /// 题型数据源
        /// </summary>
        /// <param name="examNum">试卷编号</param>
        public static object GetTXSource(string examNum)
        {
            object obj=new object();
            try
            {
                obj = from p in db.TX where p.examNum == decimal.Parse(examNum) select new { p.txNum, p.txName, p.fullScore, p.timeArea, p.bz, p.singleScore, p.tl };
            }
            catch { }
            return obj;
        }

        /// <summary>
        /// 添加新的题型
        /// </summary>
        /// <param name="newType">新的题型数据项</param>
        public static bool Add(TX newType)
        {
            bool bl = false;
            try
            {
                if (db.TX.Single(p => p.txName == newType.txName && p.examNum==newType.examNum).txNum > -1)
                    return false;
            }
            catch { }
            db.TX.InsertOnSubmit(newType);
            db.SubmitChanges();
            bl = true;
            return bl;
        }

        /// <summary>
        /// 更新题型数据项
        /// </summary>
        /// <param name="typeId">需要更新的题型标识</param>
        /// <param name="newType">新的题型数据项</param>
        public static bool Update(string typeId, TX newType)
        {
            bool bl = false;
            try
            {
                TX type = db.TX.Single(p => p.txNum == decimal.Parse(typeId));
                type.tl = newType.tl;
                type.txName = newType.txName;
                type.singleScore = newType.singleScore;
                type.bz = newType.bz;
                type.timeArea = newType.timeArea;
                type.fullScore = newType.fullScore;
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }

        /// <summary>
        /// 删除题型数据项
        /// </summary>
        /// <param name="typeNum">要删除的题型的标识</param>
        public static bool DeleteEnType(string typeNum)
        {
            bool bl = false;
            try
            {
                TX type = db.TX.Single(p => p.txNum == decimal.Parse(typeNum));
                db.TX.DeleteOnSubmit(type);
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }
    }
}
